<template>
  <div class="register">
    <v-header :title="this.$route.meta.title"></v-header>
    <div class="register-header">
      <img
        src="https://accountstatic.vivo.com.cn/accountstatic.vivo.com.cn/static/img/logo.3c33210.png.webp"
        alt=""
      />
      <span>账号注册</span>
    </div>
    <div class="register-input">
      <label for>
        <span>账号</span>
        <input
          type="text"
          v-model="user.name"
          placeholder="请输入账号/用户名"
        />
      </label>

      <label for>
        <span>密码</span>
        <input
          type="password"
          v-model="user.password"
          placeholder="请输入密码"
          ref="psdRef"
        />
        <img
          src="../../../../static/img/by.png"
          alt=""
          class="psd"
          @click="changePsd"
          ref="ImgRef"
        />
      </label>

      <label for>
        <span>手机号</span>
        <input
          type="text"
          v-model="user.phone"
          placeholder="请输入手机号"
          @blur="blur"
        />
      </label>
    </div>

    <div class="register-text">
      <van-checkbox v-model="checked"></van-checkbox>
      我已阅读并接受
      <span>《服务协议》</span>和<span>《隐私政策》</span>
    </div>
    <div class="register-btn">
      <input type="button" class="btn" value="注册" @click="register()" />
    </div>
  </div>
</template>
<script>
import header from "@/components/header/index.vue";
export default {
  components: {
    "v-header": header,
  },
  data() {
    return {
      user: {
        name: "",
        password: "",
        phone: "",
      },
      checked: false,
      phoneFlag: false,
      pwdFlag: false,
      account: JSON.parse(localStorage.getItem("user") || "[]"),
    };
  },
  methods: {
    blur() {
      if (
        !/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(
          this.user.phone
        )
      ) {
        this.$toast({
          position: "middle",
          message: "请输入正确的手机号",
        });
      } else {
        this.phoneFlag = true;
      }
    },
    changePsd() {
      if (!this.pwdFlag) {
        this.$refs.psdRef.type = "text";
        this.$refs.ImgRef.src = require("../../../../static/img/zy.png");
        this.pwdFlag = true;
        return;
      }
      if (this.pwdFlag) {
        this.$refs.psdRef.type = "password";
        this.$refs.ImgRef.src = require("../../../../static/img/by.png");
        this.pwdFlag = false;
        return;
      }
    },
    register() {
      const account = JSON.parse(localStorage.getItem("user") || "[]");
      if (!this.checked) {
        this.$toast({
          position: "middle",
          message: "请阅读并接受",
        });
        return;
      }
      if (this.user.name == "") {
        this.$toast({
          position: "middle",
          message: "用户名不能为空",
        });
        return;
      }

      if (this.user.password == "") {
        this.$toast({
          position: "middle",
          message: "密码不能为空",
        });
        return;
      }
      if (this.user.phone == "") {
        this.$toast({
          position: "middle",
          message: "手机号不能为空",
        });
        return;
      }
      if (!this.phoneFlag) {
        this.$toast({
          position: "middle",
          message: "请输入正确的手机号",
        });
        return;
      }
      const userFlag = account.some((item) => {
        return this.user.name == item.name;
      });
      if (userFlag) {
        this.$toast({
          position: "middle",
          message: "该用户已注册",
        });
        return;
      }
      if (this.phoneFlag) {
        this.$toast({
          position: "middle",
          message: "注册成功",
        });
        let arr = [];
        arr.push(this.user);
        const userList = (this.account = arr.concat(this.account));

        // await this.$http.postUser(this.user)
        localStorage.setItem("user", JSON.stringify(userList));
        this.$router.push("/login");
      }
    },
  },
};
</script>
<style lang="scss">
.register {
  background: #fff;
  .register-header {
    margin-top: 100px;
    width: 100%;
    height: 100%;
    img {
      width: 112px;
      height: 30px;
      display: block;
      margin: 40px auto 30px;
    }
    span {
      color: black;
      display: block;
      text-align: center;
      font-size: 18px;
      line-height: 18px;
    }
  }
  .register-input {
    margin: 22px 0;
    label {
      width: 90%;
      margin: auto;
      height: 56px;
      border-bottom: 1px solid #f2f2f2;
      display: flex;
      position: relative;
      span {
        width: 20%;
        font-weight: bold;
        font-size: 16px;
        line-height: 56px;
        display: block;
        float: left;
      }
      input {
        width: 80%;
        font-size: 16px;
        border: 0;
        padding-left: 12px;
      }
      .psd {
        display: block;
        width: 26px;
        height: 26px;
        position: absolute;
        right: 20px;
        top: 15px;
      }
    }
  }
  .register-text {
    display: flex;
    justify-content: center;
    font-size: 12px;
    span {
      color: blue;
    }
    .van-checkbox {
      margin-right: 10px;
    }
  }
  .register-btn {
    display: flex;
    margin-top: 20px;
    .btn {
      width: 338px;
      height: 42px;
      margin: 0 auto 12px;
      text-align: center;
      line-height: 42px;
      font-size: 16px;
      color: #fff;
      border-radius: 21px;
      border: 0;
      background-color: #46a1fa;
      background-image: linear-gradient(90deg, #418eff, #4566ff);
    }
  }
}
</style>
